#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "poller_protocol.h"

bool ValidAnswer ( char *answer );

int main ( int argc, char *argv[] )
{
	struct sockaddr_in server;
	struct sockaddr *serverptr;
	int port, serverlen, sock;
	char buf[256];
	struct hostent *rem;

	if ( argc < 3 )
	{
		printf ( "not enough arguments" );
		return EXIT_FAILURE;
	}
	if ( ( sock = socket(PF_INET, SOCK_STREAM, 0 ) ) < 0 )
	{
		perror ( "socket" );
		return EXIT_FAILURE;
	}
	if ( ( rem = gethostbyname ( argv[1] ) ) == NULL )
	{
		perror("gethostbyname");
		return EXIT_FAILURE;
	}
	bcopy ( (char *) rem->h_addr, (char *) &server.sin_addr,
		rem->h_length );
	server.sin_family = PF_INET;
	port = atoi ( argv[2] );
	server.sin_port = htons(port);
	serverptr = (struct sockaddr *) &server;
	serverlen = sizeof(server);

	if ( connect ( sock, serverptr, serverlen) < 0 )
	{
		perror("connect");
		return EXIT_FAILURE;
	}

	while ( true )
	{
		int header;
		char message[MESSAGE_SIZE];

		/* Wait for server's response */

		if ( WaitResponse ( &header, message, sock ) == false )
		{
			goto EXIT;
		}

		/* Handle the message */

		switch ( header )
		{
			int choice;
			char username[MESSAGE_SIZE], answer[MESSAGE_SIZE];

			case MENU:
				puts ( message );
				scanf ( "%d", &choice );

				switch ( choice )
				{
					case 1:
						if ( Send ( VOTE_BEGIN, "no payload", sock ) == false )
						{
							goto EXIT;
						}
						break;
					case 2:
						if ( Send ( POLL_RESULTS_BEGIN, "no payload", sock ) == false )
						{
							goto EXIT;
						}

						break;			
					case 3:
						if ( Send ( QUIT_CONNECTION, "no payload", sock ) == false )
						{
							goto EXIT;
						}
						system ( "clear" );
						goto EXIT;
					default:
						fprintf ( stderr, "You choice must be between 1 and 3\n" );
				}				
				break;
			case CREDENTIALS_REQUEST:
				system ( "clear" );
				puts ( message );
				scanf ( "%s", username );
				if ( Send (  CREDENTIALS_RESPONSE, username, sock ) == false )
				{
					goto EXIT;
				}
				break;
			case VOTE_SUBJECT:
				do
				{
					system ( "clear" );
					puts ( message );
					printf ( "Please, answer with 'yes' or 'no: " );
					scanf ( "%s", answer );
				}while ( ValidAnswer ( answer ) == false );
	
				if ( Send ( VOTE, answer, sock ) == false )
				{
					goto EXIT;
				}
				break;
			case VOTE_REGISTERED:
				system ( "clear" );
				puts ( message );
				break;
			case ERROR_VOTE_AGAIN:
			case ERROR_NOT_VOTED:
				system ( "clear" );
				puts ( message );
				break;
			case POLL_RESULTS:
				system ( "clear" );
				puts ( message );
				break;
			case QUIT_CONNECTION:
				goto EXIT;
		}
	}

EXIT:
	close ( sock );
	return 	EXIT_SUCCESS;
}

/* Checks if the answer is valid ( yes or no ) */

bool ValidAnswer ( char *answer )
{
	return ( strcmp ( answer, "YES" ) == 0 || strcmp ( answer, "yes" ) == 0 ||
		strcmp ( answer, "NO" ) == 0 || strcmp ( answer, "no" ) == 0);
}










